package cn.wjx.taobaounion.presenter.impl;

import cn.wjx.taobaounion.model.Api;
import cn.wjx.taobaounion.model.domain.OnSaleContent;
import cn.wjx.taobaounion.presenter.IOnSalePresenter;
import cn.wjx.taobaounion.utils.LogUtils;
import cn.wjx.taobaounion.utils.RetrofitManager;
import cn.wjx.taobaounion.view.IOnSalePageCallback;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static java.net.HttpURLConnection.HTTP_OK;

/**
 * @author WuChangJian
 * @date 2020/6/26 11:25
 */
public class OnSalePresenterImpl implements IOnSalePresenter {

    private static final int DEFAULT_PAGE = 1;
    private int mCurPage = DEFAULT_PAGE;
    private boolean mIsLoading = false;
    private IOnSalePageCallback mCallback;

    @Override
    public void getContent() {
        if (mIsLoading)
            return;
        mIsLoading = true;
        if (mCallback != null) {
            mCallback.onLoading();
        }
        Call<OnSaleContent> task = createTask();
        task.enqueue(new Callback<OnSaleContent>() {
            @Override
            public void onResponse(Call<OnSaleContent> call, Response<OnSaleContent> response) {
                mIsLoading = false;
                int code = response.code();
                if (code == HTTP_OK) {
                    OnSaleContent content = response.body();

                    int size = 0;
                    try {
                        size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
                    } catch (Exception e) {
                        mCallback.onEmpty();
                    }
                    if (mCallback != null) {
                        if (size > 0) {
                            mCallback.onContentLoaded(content);
                        } else {
                            mCallback.onEmpty();
                        }
                    }
                } else {
                    // 请求失败
                    LogUtils.d(OnSalePresenterImpl.this, "response code-->" + code);
                    onError();
                }
            }

            @Override
            public void onFailure(Call<OnSaleContent> call, Throwable t) {
                mIsLoading = false;
                LogUtils.e(OnSalePresenterImpl.this, "onFailure-->" + t.getMessage());
                onError();
            }
        });
    }

    private void onError() {
        if (mCallback != null) {
            mCurPage--;
            mCallback.onError();
        }
    }

    private Call<OnSaleContent> createTask() {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        return api.getOnSaleContent(mCurPage);
    }


    @Override
    public void loadMore() {
        if (mIsLoading)
            return;
        mIsLoading = true;
        mCurPage++;
        Call<OnSaleContent> task = createTask();
        task.enqueue(new Callback<OnSaleContent>() {
            @Override
            public void onResponse(Call<OnSaleContent> call, Response<OnSaleContent> response) {
                mIsLoading = false;
                int code = response.code();
                if (code == HTTP_OK) {
                    OnSaleContent content = response.body();

                    int size = 0;
                    try {
                        size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
                    } catch (Exception e) {
                        e.printStackTrace();
                        mCallback.onLoaderMoreEmpty();
                    }
                    if (mCallback != null) {
                        if (size > 0) {
                            mCallback.onLoaderMoreLoaded(content);
                        } else {
                            mCallback.onLoaderMoreEmpty();
                        }
                    }
                } else {
                    // 请求失败
                    LogUtils.d(OnSalePresenterImpl.this, "response code-->" + code);
                    loaderMoreError();
                }
            }

            @Override
            public void onFailure(Call<OnSaleContent> call, Throwable t) {
                mIsLoading = false;
                LogUtils.e(OnSalePresenterImpl.this, "onFailure-->" + t.getMessage());
                loaderMoreError();
            }
        });
    }

    private void loaderMoreError() {
        mCurPage--;
        if (mCallback != null) {
            mCallback.onLoaderMoreError();
        }
    }

    @Override
    public void reload() {
        this.getContent();
    }

    @Override
    public void registerViewCallback(IOnSalePageCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void unregisterViewCallback(IOnSalePageCallback callback) {
        if (mCallback != null) {
            mCallback = null;
        }
    }
}
